var _inherit = require('core/inherit.js');
var _UIContainer = require('component/uicontainer.js');
var _TouchNode = require('component/touchnode.js').TouchNode;
var _vec3f = require('core/glm.js').vec3f;

function FloatView(inner) {
    _UIContainer.call(this);
    this.mInner = inner;
    this.setSize(inner.getSize());
    this.addChild(inner);

    this.mMoving = false;
    this.mDown = new _vec3f();
    this.mDownPos = new _vec3f();
    this.mVector = new _vec3f();
}
_inherit(FloatView, _UIContainer);
FloatView.prototype.mTag = 'floatview';
FloatView.prototype.createEventNode = function () {
    return new _TouchNode(this, this);
};
FloatView.prototype.toString = function () {
    return this.mTag + ':' + this.mId;
};
FloatView.prototype.isInArea = function (vector) {
    vector.add(this.getCenter());
    if (vector[0] < 0 || vector[1] < 0) {
        return false;
    }
    vector.sub(this.getSize());
    if (vector[0] > 0 || vector[1] > 0) {
        return false;
    }
    return true;
};
FloatView.prototype.startMove = function (vector) {
    this.mMoving = true;
    this.mDown.set(vector);
    this.mDownPos.set(this.mInner.getPosition());
};
FloatView.prototype.moving = function (current) {
    current.sub(this.mDown);
    this.mVector.set(this.mDownPos);
    this.mVector.add(current);
    this.mInner.setPosition(this.mVector);
};
FloatView.prototype.endMove = function () {
    this.mMoving = false;
};
FloatView.prototype.onTouch = function (event) {
    this.mVector.set(event.vector);
    if (!this.isInArea(this.mVector)) {
        this.endMove();
        return false;
    }

    switch (event.state) {
        case 0:// down
            this.startMove(event.vector);
            break;
        case 1:// up
            if (this.mMoving) {
                this.moving(event.vector);
                this.endMove();
            }
            break;
        case 2:// move
            if (!this.mMoving) {
                this.startMove(event.vector);
            } else {
                this.moving(event.vector);
            }
            break;
    }
    return false;
};

module.exports = FloatView;